<?php

namespace zhtfrontend\models\datas;

use Yii;
use yii\helpers\ArrayHelper;
use common\components\behaviors\UpdateCacheBehavior;
use yii\db\ActiveRecord;
use yii\behaviors\TimestampBehavior;
use common\components\extenders\MenuManage;

/**
 * This is the model class for table "{{%classroom}}".
 *
 * @property integer $id
 * @property string $name
 * @property string $room_number
 * @property integer $max_count
 * @property integer $mechanic_id
 * @property double $area
 * @property string $info
 * @property integer $state
 * @property integer $user_id
 * @property integer $created_at
 * @property integer $classroom_cate
 * @property integer $classroom_cates
 * @property integer $share_count
 * @property integer $is_share
 */
class Classroom extends \zhtfrontend\components\bases\BaseModel
{

    public $classroom_cate;
    public $classroom_cates;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return '{{%classroom}}';
    }


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['max_count', 'state', 'area', 'info', 'name', 'room_number', 'type'], 'required'],
            [['max_count', 'state', 'user_id', 'created_at','area','share_count','classroom_group_id','is_share'], 'integer'],
            [['area'], 'number'],
            [['info', 'type'], 'string'],
            [['name', 'room_number'], 'string', 'max' => 255],
            //[['mechanic_id'],'check_Mechanic','on'=>['create','update']],
            //['type','string','min'=>4,'max'=>50],
            ['max_count', 'compare', 'compareValue' => 0, 'operator' => '>='],
            ['max_count', 'compare', 'compareValue' => 500, 'operator' => '<='],
            ['area', 'compare', 'compareValue' => 0, 'operator' => '>='],
            ['area', 'compare', 'compareValue' => 1000, 'operator' => '<='],
            ['classroom_group_id','required', 'when'=>function(){
                return $this->is_share == 1;
            }],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', 'ID'),
            'name' => Yii::t('zhtfrontend', '教室名'),
            'room_number' => Yii::t('zhtfrontend', '教室编码'),
            'max_count' => Yii::t('zhtfrontend', '最大人数'),
            'type' => Yii::t('zhtfrontend', '教室类型'),
            'mechanic_id' => Yii::t('zhtfrontend', '所属机构'),
            'area' => Yii::t('zhtfrontend', '教室面积'),
            'info' => Yii::t('zhtfrontend', '教室介绍'),
            'state' => Yii::t('zhtfrontend', '状态'),
            'user_id' => Yii::t('zhtfrontend', '创建人'),
            'created_at' => Yii::t('zhtfrontend', '创建时间'),
            'classroom_cate' => Yii::t('zhtfrontend','教室分类'),
            'classroom_cates' => Yii::t('zhtfrontend','已选教室分类'),
            'share_count' => Yii::t('zhtfrontend','同时段使用量'),
            'is_share' => Yii::t('zhtfrontend','是否共享'),
            'classroom_group_id' => Yii::t('zhtfrontend','教室组'),
        ];
    }

    public function extraFields()
    {
        return [
            'admin' => function () {
                return isset($this->admin->username)?$this->admin->username:'';
            },
            'zhtfrontend_operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->isDisable(
                    ['delete' => function ($data) {
                        $classroom_id =Arrangement::find()->select('classroom_id')->where("classroom_id!=''")->andWhere(['is_delete'=>0])->distinct('classroom_id')->column();
                        if ($classroom_id != null && in_array($data['id'], $classroom_id)) {
                            return '<button class="yx-btn-disabled" disabled>删除</button>';
                        } else {
                            return '';
                        }
                    },
                        'update' => function ($data) {
                            return '';
                        }
                    ]
                )->create();
            },

        ];
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['state'] = function () {
            switch ($this->state) {
                case 0:
                    return \Yii::t('zhtfrontend', '未启用');
                case 1:
                    return \Yii::t('zhtfrontend', '启用');
            }
        };
        $fields['created_at'] = function () {
            return \Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => UpdateCacheBehavior::className(),
                'cacheName' => 'classroom',
            ],
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],
        ]);
    }

    public function check_Mechanic($attribute){
        $mechanic = Mechanic::find()->where(['parent_id'=>$this->mechanic_id])->all();
        if($mechanic != null){
            $this->addError($attribute, Yii::t('zhtfrontend', '请选择子级机构'));
        }

    }

    public function getAdmin()
    {
        return $this->hasOne(Admin::className(), ['id' => 'user_id']);
    }

    public function getMechanic()
    {
        return $this->hasOne(Mechanic::className(), ['id' => 'mechanic_id']);
    }
}
